/**
  * @file:     ringBuff.c
  * @author:   AresXu
  * @version:  v1.0.0
  * @date:     2019-11-xx
  * @brief:    环形队列
*/
#include "ringBuff.h"
#include "stdio.h"


/**
  * @brief:        判斷環形隊列是否空
  * @param[in]:     None
  * @retval[out]:   None
  * @author:       AresXu
  * @version:      v1.0.0
*/
bool IsRingBufferEmpty(stRingBuff *ringBuf)
{
    if (ringBuf == NULL)
    {
        printf("pointer is null\r\n");
        return;
    }
    
    if(ringBuf->in == ringBuf->out)
    {
//		printf("Ring buffer is Empty\r\n");
        return TRUE;
    }
    return FALSE;
}

/**
  * @brief:         判斷環形隊列是否满
  * @param[in]:     None
  * @retval[out]:   None
  * @note:            
  * @author:       AresXu
  * @version:      v1.0.0
*/
bool IsRingBufferFull(stRingBuff *ringBuf)
{
    if (ringBuf == NULL)
    {
        printf("pointer is null\r\n");
        return;
    }

    if(((ringBuf->in+1) % RING_BUFF_SIZE) == ringBuf->out)
    {
//		printf("Ring buffer is Full\r\n");
        return TRUE;
    }
    return FALSE;
}

/**
  * @brief:         寫一字節的數據到環形隊列
  * @param[in]:     None
  * @retval[out]:   None
  * @note:            
  * @author:       AresXu
  * @version:      v1.0.0
*/
char WriteOneByteToRingBuffer(stRingBuff *ringBuf,char data)
{
    if (ringBuf == NULL)
    {
        printf("pointer is null\r\n");
        return;
    }

    if(IsRingBufferFull(ringBuf))
    {
        return FALSE;
    }

    ringBuf->buffer[ringBuf->in] = data;
    ringBuf->in = (++ringBuf->in) % RING_BUFF_SIZE;    //防止越界
	return TRUE;
}

/**
  * @brief:         从環形隊列中读一字节数据
  * @param[in]:     None
  * @retval[out]:   None
  * @note:            
  * @author:       AresXu
  * @version:      v1.0.0
*/
char ReadOneByteFromRingBuffer(stRingBuff *ringBuf,char *data)
{
    if (ringBuf == NULL)
    {
        printf("pointer is null\r\n");
        return;
    }

    if(IsRingBufferEmpty(ringBuf))
    {
        return FALSE;
    }

    *data = ringBuf->buffer[ringBuf->out];
    ringBuf->out = (++ringBuf->out) % RING_BUFF_SIZE;    //防止越界

    return TRUE;
} 

/**
  * @brief:         寫len個字節數據到環形隊列
  * @param[in]:     None
  * @retval[out]:   None
  * @note:            
  * @author:        AresXu
  * @version:       v1.0.0
*/
void WriteRingBuffer(stRingBuff *ringBuf,char *writeBuf,unsigned int len)
{
    unsigned int i;

    if (ringBuf == NULL)
    {
        printf("pointer is null\r\n");
        return;
    }

    for(i = 0; i < len; i++)
    {
        WriteOneByteToRingBuffer(ringBuf,writeBuf[i]);
    }
}

/**
  * @brief:         從環形隊列讀出len個字節的數據
  * @param[in]:     None
  * @retval[out]:   None
  * @note:            
  * @author:       AresXu
  * @version:      v1.0.0
*/
void ReadRingBuffer(stRingBuff *ringBuf,char *readBuf,unsigned int len)
{
    unsigned int i;

    if (ringBuf == NULL)
    {
        printf("pointer is null\r\n");
        return;
    }
	
    for(i = 0; i < len; i++)
    {
        ReadOneByteFromRingBuffer(ringBuf,&readBuf[i]);
    }
}


/**
  * @brief:         獲取已經寫入的長度
  * @param[in]:     None
  * @retval[out]:   None
  * @note:            
  * @author:        AresXu
  * @version:       v1.0.0
*/
int GetRingBufferLength(stRingBuff *ringBuf)
{
    if (ringBuf == NULL)
    {
        printf("pointer is null\r\n");
        return;
    }

    return (ringBuf->in - ringBuf->out + RING_BUFF_SIZE) % RING_BUFF_SIZE;
}